<?php

namespace app\commonapi\controller;

use think\Controller;
use app\common\model\AreasModel;

class Common extends Controller {

	protected $areas_model;
	protected $parm;

	public function _initialize() {
		parent::_initialize();
		$param = file_get_contents("php://input");
		$this->parm = json_decode($param, true); //接收传过来的数据
		$this->areas_model = new AreasModel();
	}

	/**
	 * showdoc
	 * @catalog API文档/公共相关
	 * @title 发送短信验证码
	 * @description 发送短信验证码
	 * @method post
	 * @url  /commonapi/Common/sendSms
	 * @param phone 必选 string 手机号码 
	 * @return {"code":1000,"message":"短信发送成功,3分钟内有效1513","data":1513}
	 * @return_param data int 验证码
	 * @remark 
	 * @number 99
	 * @author lcx
	 * @date 2019-03-20
	 */
	public function sendSms() {
		if (empty($this->parm['phone'])) {
			returnJson(-1002, '手机号不能为空');
		}
		if (!inge_ismobile($this->parm['phone'])) {
			returnJson(-1002, '手机号不合法');
		}
		$yzm = rand(1000, 9999);
		$result_data = sendSms($this->parm['phone'], "您的验证码:" . $yzm);
		if ($result_data['code'] == 0) {
			// 设置缓存的同时并且进行参数设置
			cache($this->parm['phone'], $yzm, 1800);
			returnJson(1000, '短信发送成功,3分钟内有效');
		} else {
			returnJson(-1000, '短信发送失败');
		}
	}

	/**
	 * showdoc
	 * @catalog API文档/公共相关
	 * @title 短信验证码校验
	 * @description 验证验证码是否正确
	 * @method post
	 * @url  /commonapi/Common/checkSmsCode
	 * @param phone 必选 string 手机号码
	 * @param code 必选 string 验证码
	 * @return {"code":1000,"message":"验证成功"}
	 * @remark 
	 * @number 99
	 * @author lcx
	 * @date 2019-03-20
	 */
	public function checkSmsCode() {
		if (empty($this->parm['phone'])) {
			returnJson(-1002, '请输入手机号');
		}
		if (!inge_ismobile($this->parm['phone'])) {
			returnJson(-1002, '手机号不合法');
		}
		if (empty($this->parm['code'])) {
			returnJson(-1002, '请输入验证码');
		}
		if ($this->parm['code'] != cache($this->parm['phone'])) {
			returnJson(-1100, '验证码错误');
		}
		returnJson(1000, "验证成功");
	}

	/**
	 * showdoc
	 * @catalog API文档/公共相关
	 * @title 获取区域列表
	 * @description 默认查询一级地区
	 * @method post
	 * @url  /commonapi/Common/getArea
	 * @param pid 可选 int 父级ID,默认0
	 * @return {"code":1000,"msg":"获取成功","data":[{"id":2,"parent_id":0,"area_name":"北京市"},{"id":3,"parent_id":0,"area_name":"天津市"}]}
	 * @return_param id int 区域ID
	 * @return_param parent_id int 父级ID
	 * @return_param area_name string 区域名称
	 * @remark 
	 * @number 99
	 * @author lcx
	 * @date 2019-03-21
	 */
	public function getArea() {
		$map['parent_id'] = $this->parm['pid']??0;
		$data = $this->areas_model->getAll($map);
		if (empty($data)) {
			returnJson(-1003, "暂无数据");
		}
		returnJson(1000, "获取成功", $data);
	}

}
